package Utils.jsoup;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Jsoup工具类
 *
 * @author d11yu
 */
public final class JsoupUtils {

    public static final String USER_AGENT_1 = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";

    /**
     * 一个完整的process，包括开启和关闭httpClient、response。
     *
     * @param actionProc ActionProc委托
     * @throws IOException 异常
     */
    public static void processWhole(ActionProc actionProc) throws IOException {
        //1.生成httpclient，相当于该打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        //2.创建get请求，相当于在浏览器地址栏输入 网址
        HttpGet request = null;

        try {
            actionProc.invoke(httpClient, response, request);
        } finally {
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }
    }

    /**
     * 获取get request
     *
     * @param url url字符串
     * @return
     */
    public static HttpGet getRequest(String url) {
        HttpGet request = new HttpGet(url);
        request.setHeader("User-Agent", USER_AGENT_1);
        return request;
    }

    /**
     * 返回response
     *
     * @param url
     * @param client 可以使用默认实例：HttpClients.createDefault()
     * @return
     * @throws IOException
     */
    public static CloseableHttpResponse getResponse(String url, CloseableHttpClient client) throws IOException {
        return getResponse(getRequest(url), client);
    }

    /**
     * 返回response
     *
     * @param request
     * @param client  可以使用默认实例：HttpClients.createDefault()
     * @return
     * @throws IOException
     */
    public static CloseableHttpResponse getResponse(HttpGet request, CloseableHttpClient client) throws IOException {
        return client.execute(request);
    }

    /**
     * 获取Html内容（字符串格式）
     *
     * @param response
     * @return
     * @throws IOException
     */
    public static String getHtmlString(CloseableHttpResponse response) throws IOException {
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    }

    /**
     * 获取Html内容（字符串格式）
     *
     * @param response
     * @param charset
     * @return
     * @throws IOException
     */
    public static String getHtmlString(CloseableHttpResponse response, Charset charset) throws IOException {
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity, charset);
    }

    /**
     * 获取Html内容（字符串格式）
     *
     * @param url
     * @param client 可以使用默认实例：HttpClients.createDefault()
     * @param charset
     * @return
     * @throws IOException
     */
    public static String getHtmlString(String url, CloseableHttpClient client, String charset) throws IOException {
        return getHtmlString(getResponse(url, client), charset);
    }

    /**
     * 获取Html内容（字符串格式）
     *
     * @param response
     * @param charset
     * @return
     * @throws IOException
     */
    public static String getHtmlString(CloseableHttpResponse response, String charset) throws IOException {
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity, charset);
    }
}
